<template>
  <div id="container">
    <br>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="投诉人姓名" prop="complaintName">
        <el-input v-model="queryParams.complaintName" placeholder="请输入投诉人的姓名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="投诉人手机号" prop="complaintPhone">
        <el-input v-model="queryParams.complaintPhone" placeholder="请输入投诉人的手机号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="投诉标题" prop="cTitle">
        <el-input v-model="queryParams.cTitle" placeholder="请输入投诉标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="受理分类" prop="cType">
        <el-select v-model="queryParams.cType" placeholder="这是是分类的查询" clearable>
          <el-option v-for="dict in dict.type.c_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:complain:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:complain:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-upload2" size="mini" @click="handleImport">导入</el-button></el-col>


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      <!-- <el-badge :value="weidu" class="item">
        <el-button size="small">未处理</el-button>
      </el-badge> -->
    </el-row>
    <div id="imge"></div>
    <el-table v-loading="loading" :data="complainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号"  align="center" width="60px" prop="complaintId" /> -->
      <el-table-column prop="complaintName" label="投诉人姓名" align="center">
        <template slot-scope="scope">
          {{ scope.row.complaintName ? scope.row.complaintName.replace(/(?<=[\u4e00-\u9fa5]).*(?=[\u4e00-\u9fa5])/, "*") : "" }}
        </template>
      </el-table-column>      <!-- <el-table-column label="投诉人手机号" align="center" prop="complaintPhone" /> -->

      <el-table-column prop="complaintPhone" label="用户手机号" align="center">
        <template slot-scope="scope">
          {{ scope.row.complaintPhone ? scope.row.complaintPhone.replace(/(\d{3})(\d{4})(\d{4})/, "$1****$3") : "" }}
        </template>
      </el-table-column>

      <el-table-column label="用户邮箱" align="center" prop="mailbox" :show-overflow-tooltip="true" />
      <el-table-column label="投诉标题" :show-overflow-tooltip="true" align="center" prop="cTitle" />
      <el-table-column label="图片凭证" align="center" prop="companySrc" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.companySrc" :width="50" :height="50" />
        </template>
      </el-table-column>

      <el-table-column label="管理员编号" width="150px" align="center" prop="adnameId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.adname_id" :value="scope.row.adnameId" />
        </template>
      </el-table-column>

      <el-table-column label="被投诉人名字" width="100 px" align="center" prop="adnameName" />
      <!-- <el-table-column label="被投诉人单位电话" align="center" :show-overflow-tooltip="true" prop="adphone" /> -->

      <el-table-column prop="adphone" label="被投诉人手机号" align="center">
        <template slot-scope="scope">
          {{ scope.row.adphone ? scope.row.adphone.replace(/(\d{3})(\d{4})(\d{4})/, "$1****$3") : "" }}
        </template>
      </el-table-column>


      <el-table-column label="投诉内容" :show-overflow-tooltip="true" align="center" prop="companyContent" />

      <el-table-column label="处理状态" align="center" prop="cType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_type" :value="scope.row.cType" />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button size="mini" type="text" @click="xiangqing(scope.row)">内容详情</el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:complain:edit']">受理</el-button>


          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:complain:remove']">删除</el-button>


        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改投诉受理管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      

        <el-form-item label="理由" prop="companyText">
          <el-input style="font-size: 20px;"  type="textarea" v-model="form.companyText" :rows="8"  placeholder="请输入您处理or婉拒的理由" />
        </el-form-item>

        <!-- <el-form-item label="管理员" prop="company_shouliname">
          <el-select v-model="form.company_shouliname" placeholder="您是哪个管理员？">
            <el-option v-for="dict in dict.type.company_shouliname" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item> -->

        <el-form-item label="您好">
          <el-input type="text" readonly="readonly" v-model="form.nickName" />
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="submitForm1()" style="color: aqua;">受理</el-button>
        <el-button @click="submitForm2()" style="color: red;">驳回</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!-- 对话框:点击新增或编辑才会弹出表单 -->
    <el-dialog title="" :visible.sync="dialogVisible">
      <h1 style="text-align: center;">投诉内容</h1>

      <!-- 这里放Form表单的代码 -->
      <div slot="footer" class="dialog-footer">

        <el-form ref="form">

          <el-input style="font-size: 20px;" type="textarea" :rows="10" v-model="form.companyContent" readonly="readonly"
            :min-height="192" />

        </el-form>
        <br>
        <el-button style="background-color: aquamarine;" @click="dialogVisible = false">查看完毕</el-button>

      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listComplain, getComplain, delComplain, addComplain, updateComplain } from "@/api/system/complain";
import { getToken } from "@/utils/auth";
// 导入模板接口importTemplate
import { importTemplate } from "@/api/system/user";

export default {
  name: "Complain",
  dicts: ['c_type', 'company_shouliname', 'adname_id'],
  data() {
    return {

      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/complain/importData"
      },
      weidu:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投诉受理管理表格数据
      complainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        complaintName: null,
        complaintPhone: null,
        mailbox: null,
        cTitle: null,
        companySrc: null,
        companyContent: null,
        companyName: null,
        companyPhone: null,
        cType: null,
        companyText: null,
        company_shouliname: null,
        nickName: null
      },
      // 表单参数
      form: {
        showCom:null
      },
      // 表单校验
      rules: {
        companyText: [
          { required: true, message: '内容不能为空', trigger: 'blur' },

        ]

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        this.download(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },


    xiangqing(row) {
      this.reset();
      const complaintId = row.complaintId || this.ids
      getComplain(complaintId).then(response => {
        this.form = response.data;
        this.title = "详情";
      });
      this.dialogVisible = true;
    },
    /** 查询投诉受理管理列表 */
    getList() {
      this.loading = true;
      listComplain(this.queryParams).then(response => {
        this.complainList = response.rows;
        console.log(this.complainList);
        this.total = response.total;
        this.loading = false;
        for (let index = 0; index < this.complainList.length; index++) {
            if(this.complainList[index].cType==1){
              this.weidu++;
            }
        }

      });
    },
    getListTwo() {
      this.loading = true;
      listComplain(this.queryParams).then(response => {
        this.complainList = response.rows;
        console.log(this.complainList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        complaintId: null,
        complaintName: null,
        complaintPhone: null,
        mailbox: null,
        cTitle: null,
        companySrc: null,
        companyContent: null,
        companyName: null,
        companyPhone: null,
        cType: null,
        companyText: null,
        company_shouliname: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if(this.weidu==0){
        this.getList()
      }else{
        this.getListTwo();

      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      if(this.weidu==0){
        this.getList()
      }else{
        this.getListTwo();

      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.complaintId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投诉受理管理";
    },
    /** 投诉按钮操作 */
    handleUpdate(row) {

      if (row.cType == 0 || row.cType == 2) {
        this.$modal.msgSuccess("已经处理过该受理哦~~~~~");
      } else {
        this.reset();
        const complaintId = row.complaintId || this.ids
        getComplain(complaintId).then(response => {
          this.form = response.data;
          console.log(this.form);
          this.open = true;
          this.title = "投诉受理批改";
        });
      }

    },
    submitForm2() {
      this.form.cType = 2;
      this.form.showCom= 1;
      console.log(this.form.showCom);
      // this.form.companyText = null,
        this.$refs["form"].validate(valid => {
          updateComplain(this.form).then(response => {
            this.$modal.msgSuccess("审批成功");
            this.open = false;
                });
        });
    },
    /** 提交按钮 */
    submitForm1() {
      this.form.cType = 0;
      this.form.showCom= 1;
      // this.form.companyText = null,
        this.$refs["form"].validate(valid => {
          updateComplain(this.form).then(response => {
            this.$modal.msgSuccess("审批成功");
            this.weidu--;
            this.open = false;
           this. getListTwo()
              });
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {

      const complaintIds = row.complaintId || this.ids;
      this.$modal.confirm('是否确认删除投诉受理管理编号为"' + complaintIds + '"的数据项？').then(function () {
        return delComplain(complaintIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });


    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/complain/export', {
        ...this.queryParams
      }, `complain_${new Date().getTime()}.xlsx`)
    }
  },

}

</script>
<style>
#imge {
  background-image: url("../../../assets/ku/ku2.png");
  justify-content: center;
  background-repeat: no-repeat;
  height: 50px;
  width: 50px;
  background-size: 100%;
  padding: 0;
  margin: 0;
  position: relative;
  left: 780px;
  top: 10px;
}
</style>
